<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>机构管理</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

</head>

<body>
	<%@ include file="/WEB-INF/jsp/common/header.jsp"%>

	<script>
		$(function() {
			//alert(test);
			console.log('loginname' + PUser[0].loginname);
			loadCombotree();
			bindtreegrid();
		});
	</script>
	<script>
		function editrow() {
			var node = $('#tt').treegrid('getSelected');
			if (node) {
				//修改
				$("#t1").val(node.dwbm);
				if (node.dwbm == PUser[0].userdept) {
					$.messager.alert('消息', '不可编辑本单位');
					return;
				}
				$('#txtBM').val(node.dydwbm);
				//$('#t1').attr("disabled", "disabled");
				$('#t2').val(unescape(node.dwmc));
				$('#t3').combotree('setValue', node.parentid);
				$('#t3').combotree('enable');
				//if (node.DISABLED == "1")
				//$('#t4').attr('checked', true);
				//else
				//$('#t4').attr('checked', false);
				//alert(node.XSSX);
				$('#t5').numberbox('setValue', node.xssx);
				//alert(unescape(node.JGSX));
				$('#tJGSX').val(unescape(node.jgsx));
				$('#add').window('open');
			} else
				$.messager.alert('消息', '请选中一个机构');
		}
		function save() {
			if (!$('#ssec-form').form('validate')) {
				return;
			}
			var node = $('#tt').treegrid('getSelected');
			var a = $('#t1').val();
			var b = $('#t2').val();
			var c = $('#t3').combotree('getValue');
			var d = null;
			var e = $('#t5').val();
			var f = $('#txtBM').val();
			var jgsx = $('#tJGSX').val();
			console.log('loginname' + PUser[0].loginname);
			$.ajax({
				type : "post",
				url : "DeptManage/AddDept",
				//                dataType: "xml",
				data : {
					dwbm : a,
					dwmc : b,
					pid : c,
					dydwbm : f,
					xssx : e,
					jgsx : jgsx,
					userid : PUser[0].loginname
				},
				success : function(data) {
					$.messager.alert('消息', data);
					if (data == '保存成功!') {
						$('#add').window('close');
						bindtreegrid();
					}
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					$.messager.alert('消息', errorThrown);
				}
			});
		}
		function del(node) {
			$.ajax({
				type : "post",
				url : "DeptManage/DelDept",
				//                dataType: "xml",
				data : {
					dwbm : node.dwbm,
					userid : PUser[0].loginname
				},
				success : function(data) {
					$.messager.alert('消息', data);
					$('#add').window('close');
					bindtreegrid();
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					$.messager.alert('消息', errorThrown);
				}
			});
		}
	</script>
	<script type="text/javascript">
		function loadCombotree() {
			console.log(PUser[0]);
			$('#t3').combotree(
					{
						url : 'DeptManage/DeptListJson_combobox?dwbm='
								+ PUser[0].userdept
					});
		}

		function bindtreegrid() {
			$('#tt')
					.treegrid(
							{
								title : '组织机构列表',
								url : 'DeptManage/DeptListJson?dwbm='
										+ PUser[0].userdept,
								iconCls : 'icon-applicationgo',
								nowrap : true,
								width : 1000,
								height : 500,
								rownumbers : true,
								animate : false,
								collapsible : false,
								singleSelect : true,
								remoteSort : false,
								idField : 'dwbm',
								treeField : 'dwmc',
								columns : [ [ {
									field : 'dwbm',
									title : '机构编码',
									hidden : true,
									width : 100,
									formatter : function(val, rec) {
										return unescape(val)
									}
								}, {
									field : 'dydwbm',
									title : '机构编码',
									width : 100,
									formatter : function(val, rec) {
										return unescape(val)
									}
								}, {
									field : 'dwmc',
									title : '名称',
									sortable : true,
									width : 200,
									formatter : function(val, rec) {
										return unescape(val)
									}
								}, {
									field : 'xssx',
									title : '排序',
									sortable : true,
									width : 100,
									formatter : function(val, rec) {
										return unescape(val)
									}
								}, {
									field : 'jgsx',
									title : '机构属性',
									sortable : true,
									width : 300,
									formatter : function(val, rec) {
										return unescape(val)
									}
								} ] ],
								toolbar : [
										{
											text : '新增同级机构',
											//disabled: Permission.indexOf('显示') > 0 ? false : true,
											iconCls : 'icon-add',
											handler : function() {
												$('#ssec-form').form('clear');
												var node = $('#tt').treegrid(
														'getSelected');
												if (node != null) {
													//弹出窗口
													if (node.dwbm == PUser[0].userdept) {
														$.messager.alert('消息',
																"不可以在本级增加同级机构");
														return;
													}
													$('#t3').combotree(
															'setValue',
															node.parentid);
												}
												$('#add').window('open');
											}
										},
										'-',
										{
											text : '增加下级机构',
											iconCls : 'icon-add',

											handler : function() {
												$('#ssec-form').form('clear');
												var node = $('#tt').treegrid(
														'getSelected');
												if (node != null) {
													//弹出窗口
													$('#t3').combotree(
															'setValue',
															node.dwbm);
												}
												$('#add').window('open');
											}
										},
										'-',
										{
											text : '修改机构',

											iconCls : 'icon-edit',
											handler : function() {
												editrow();
											}
										},
										'-',
										{
											text : '删除机构',
											iconCls : 'icon-remove',
											handler : function() {
												var node = $('#tt').treegrid(
														'getSelected');
												if (node.dwbm == PUser[0].userdept) {
													$.messager.alert('消息',
															'不可以删除此机构');
													return;
												}

												var nodes;
												if (node) {
													nodes = $('#tt').treegrid(
															'getChildren',
															node.dwbm);
													if (nodes.length > 0) {
														$.messager.alert('消息',
																'还有下级机构，不可删除');
													} else {
														$.messager
																.confirm(
																		'提示',
																		'确定要删除【'
																				+ unescape(node.dwmc)
																				+ '】这个机构吗?',
																		function(
																				r) {
																			if (r) {
																				del(node);
																			}
																		});
													}
												} else
													$.messager.alert('消息',
															'请选中一个机构');
											}
										} ],
								onLoadSuccess : function() {
									$('#tt').treegrid('collapseAll');
									var rows = $('#tt').treegrid('getChildren');
									if (rows.length) {
										$('#tt').treegrid('expand',
												rows[0].dwbm);
									}
								}
							});
		}
	</script>

	<style scoped="scoped">
.textbox {
	height: 20px;
	margin: 0;
	padding: 0 2px;
	box-sizing: content-box;
}

.table tr {
	height: 30px;
}
</style>
	<table id="tt" style="height: 500px;"></table>
	<div id="add" class="easyui-window" closed="true" modal="true"
		maximized="false" title="组织机构"
		style="width: 500px; height: 400px;padding:10px 20px">
		<form id="ssec-form" class="ssec-form">
			<table class="table">
				<tr>
					<td>
						<div>
							机构编码：<input id="t1" type="hidden" />
						</div>
					</td>
					<td>
						<div>
							<input id="txtBM" class="easyui-validatebox textbox"
								data-options="required:true" />
						</div>
					</td>
				</tr>
				<tr>
					<td>
						<div class="ssec-label">机构名称：</div>
					</td>
					<td>
						<div class="ssec-text normal">
							<input id="t2" class="easyui-validatebox textbox"
								data-options="required:true" />
						</div>
					</td>
				</tr>
				<tr>
					<td>
						<div class="ssec-label">上级机构：</div>
					</td>
					<td>
						<div>
							<input id="t3" class="easyui-combotree"
								data-options="required:true" style="width: 230px" />
						</div>
					</td>
				</tr>
				<tr>
					<td>
						<div class="ssec-label">排序：</div>
					</td>
					<td>
						<div class="ssec-text normal">
							<input id="t5" class="easyui-numberbox"
								data-options="required:true" />
						</div>
					</td>
				</tr>
				<tr>
					<td>
						<div class="ssec-label">机构属性：</div>
					</td>
					<td>
						<div class="ssec-text normal">
							<input id="tJGSX" class="easyui-validatebox textbox"
								style="width:260px;" />
						</div>
					</td>
				</tr>
			</table>
			<div style="padding: 5px;text-align:right;">
				<a class="easyui-linkbutton" iconcls="icon-save" onclick="save();">保存</a>
			</div>
		</form>
	</div>
</body>
</html>
